<?php

/**
 * Class HomeAction 控制与登录页面，会根据不同的访问设备进行跳转
 */
class HomeAction extends Action
{
    #region 首页及相关页面
    /**
     * 系统首页，会根据登录情况进行跳转
     */
    public function index()
    {
        $device = device_check();
        //处理手机和Wap访问，如果有Wap版本的页面请修改下边的逻辑
        if ($device === "classic") {
            die("暂时不支持您使用的浏览器，请升级您的设备！");
        }
        //处理爬虫程序
        if ($device === "spider") {
            die("请访问网站的robot.txt文件！");
        }
        //调试模式自动登录
        if (!session("identity") && !APP_DEBUG) {
            YHXX::login("admin", "123456", null, false);

        }
        //登录判断
        if (session("identity")) {
            $yhxx = new YHXX();
            $yhxx->fetch(session("identity"));
            $this->assign("yhxx", $yhxx); //绑定用户信息
            $this->assign("yhzxx", $yhxx->getYHZXX()); //绑定用户组信息
            //处理手机或者PAD和桌面浏览器访问
            if ($device === "phone") {
                die("手机版本开发中！");
            } elseif ($device === "pad") {
                $this->display("pad-index");
            } else {
                $this->display("index");
            }
        } else {
            //处理手机访问
            if ($device === "phone") {
                die("手机版本开发中！");
            } elseif ($device === "pad") {
                $this->display("pad-login");
            } else {
                $this->display("login");
            }
        }
    }

    public function introduce()
    {
        $cdxxx = new CDXXX();
        $result = $cdxxx->fetch($_GET["CDXXX_BH"]);
        if (empty($result)) {
            $this->assign("cdxxx", $cdxxx); //绑定菜单项信息
            $this->assign("cdxx", $cdxxx->getCDXX()); //绑定菜单信息
            $this->assign("list", ZCDXX::query(array("CDXXX_BH" => $_GET["CDXXX_BH"]))['DataList']);
            $this->display();
            return;
        }
        $cdxx = new CDXX();
        $result = $cdxx->fetch($_GET["CDXX_BH"]);
        if (empty($result)) {
            $this->assign("cdxx", $cdxx); //绑定菜单信息
            $this->assign("list", CDXXX::query(array("CDXX_BH" => $_GET["CDXX_BH"]))['DataList']);
            $this->display();
            return;
        }
        die("您传入的参数非法");

    }

    #endregion 首页及相关页面

    #region 登录及验证码功能
    /**
     * 登出页，将会退出系统并进入首页
     */
    public function logout()
    {
        YHXX::logout();
        $this->redirect(U("Home/index"));
    }

    /**
     * 登录验证页面
     */
    public function login_check()
    {
        $result = YHXX::login($_POST['username'], $_POST['password'], $_POST['verify'], false);
        $this->ajaxReturn(array("success" => empty($result), "message" => $result, "verify" => session("error") > C("AUTO_GENERATOR_VERIFYTIMES")));
    }

    /**
     * 产生验证码
     */
    Public function verify()
    {
        import('ORG.Util.Image');
        Image::buildImageVerify(5, 3, "png", 70, 25, 0, "verify");
    }

    #endregion 登录及验证码功能

    #region 文件上传与图片缩略图
    /**
     * UE使用的图片上传功能
     */
    public function ue_image()
    {
        if (!session("identity")) {
            $this->show(json_encode(array("state" => "非法操作")));
            return;
        }
        $dir = "UEImage/common";
        if (htmlspecialchars($_POST['dir'], ENT_QUOTES) === "个人可见") {
            $dir = "UEImage/" . session("identity");
        }
        if (!empty($_FILES)) {
            $result = upload($dir, 10000, array(".gif", ".png", ".jpg", ".jpeg", ".bmp"));
            $this->show(json_encode(array("url" => $result["url"], "title" => htmlspecialchars($_POST['pictitle'], ENT_QUOTES), 'original' => $result['original'], 'state' => $result["state"])));
        } else {
            $this->show(json_encode(array("state" => "您没有上传文件")));
        }
    }

    /**
     * UE文件上传功能
     */
    public function ue_file()
    {
        if (!empty($_FILES)) {
            $dir = "UEFile" . session("identity");
            $result = upload($dir, 200000, array(".rar", ".doc", ".docx", ".zip", ".pdf", ".txt", ".swf", ".wmv", ".mp3", ".mp4", ".png", ".jpg", ".3gp", ".3gpp", ".aac", ".m4a", ".amr", ".gif", ".png", ".jpg", ".jpeg", ".bmp"));
            $this->show(json_encode(array("state" => $result["state"], "fileType" => $result["type"], "url" => $result["url"], "original" => $result["original"])));
        } else {
            $this->show(json_encode(array("state" => "您没有上传文件")));
        }
    }

    /**
     * 在线图片预览
     */
    public function ue_manager()
    {
        $action = htmlspecialchars($_POST["action"]);
        if ($action == "get") {
            $files = getfiles("Uploads/UEImage/common/");
            $user = getfiles("Uploads/UEImage/" . session("identity") . "/", $files);
            if (!count($files)) return;
            rsort($user, SORT_STRING);
            $str = "";
            foreach ($user as $file) {
                $str .= $file . "ue_separate_ue";
            }
            $this->show($str);
        }
    }

    /**
     * 文件上传功能
     */
    public function upload()
    {
        if (!session("identity")) {
            $this->show(json_encode(array("success" => false, "message" => "非法操作")));
            return;
        }
        if (!empty($_FILES)) {
            $dir = $_POST["dir"];
            $result = upload($dir, 200000, array(".rar", ".doc", ".docx", ".zip", ".pdf", ".txt", ".swf", ".wmv", ".mp3", ".mp4", ".png", ".jpg", ".3gp", ".3gpp", ".aac", ".m4a", ".amr", ".gif", ".png", ".jpg", ".jpeg", ".bmp"));
            $this->show(json_encode(array("success" => $result["state"] == "SUCCESS", "message" => $result["state"], "url" => $result["url"])));
        } else {
            $this->show(json_encode(array("success" => false, "message" => "您没有上传文件")));
        }
    }

    /**
     * 文件上传功能
     */
    public function image()
    {
        if (!session("identity")) {
            $this->show(json_encode(array("success" => false, "message" => "非法操作")));
            return;
        }

        if (!empty($_FILES)) {
            $dir = $_POST["dir"];
            $result = upload($dir, 10000, array(".gif", ".png", ".jpg", ".jpeg", ".bmp"));
            $ext = strtolower(pathinfo($result["url"])['extension']);
            if ($ext != "png") {
                $url = image_thumb($result["url"]);
                if (!$url["success"]) {
                    $this->show(json_encode($url));
                    return;
                }
                $result["url"] = $url["url"];
            }
            $this->show(json_encode(array("success" => $result["state"] == "SUCCESS", "message" => $result["state"], "url" => $result["url"])));
        } else {
            $url = $_POST["url"]; //取得上传来的URL
            $width = $_POST["width"]; //宽度
            $height = $_POST["height"]; //高度
            $type = $_POST["type"]; //裁切类型
            if ($type == 0) {
                $this->show(json_encode(array("success" => true, "message" => "", "url" => $url)));
            } elseif ($type == 1 || $type == 2) {
                $pos = $_POST["pos"];
                $url = image_thumb($url, $width, $height, $type, $pos);
                $this->show(json_encode($url));
            } elseif ($type == 3) {
                $start_x = $_POST["start_x"];
                $start_y = $_POST["start_y"];
                $end_x = $_POST["end_x"];
                $end_y = $_POST["end_y"];
                $url = image_thumb($url, $width, $height, $type, 0, $start_x, $start_y, $end_x, $end_y);
                $this->show(json_encode($url));
            } else {
                $this->show(json_encode(array("success" => false, "message" => "类型未知")));
            }
        }
    }

    #endregion 文件上传与图片缩略图

}